package edu.gmu.atelier.generic;

import edu.gmu.atelier.generic.AbstractMatrix;
import java.util.ArrayList;
import java.util.List;

/**
 * Set of vectors with vector combination and scalar multiplication operations.
 * @author James H. Pope
 */
public class RealVectorFactory
{
    //------------------------------------------------------------------------//
    // Factory methods
    //------------------------------------------------------------------------//
    
    /**
     * Makes a new matrix from data array in row-major order.
     * @param data
     * @return 
     */
    public static List<RealVector> makeVectorsFromRows(double[][] data)
    {
        List<RealVector> b = new ArrayList<RealVector>();
        for (int c = 0; c < data[0].length; c++)
        {
            double[] v = new double[data.length];
            for (int r = 0; r < v.length; r++)
            {
                v[r] = data[r][c];
            }
            b.add( c, new RealVector(v) );
        }
        return b;
    }
    
    /**
     * Makes a new matrix from data array in row-major order.
     * Data is copied into a column-major order matrix.  The transformation
     * is straight forward, however inefficient, it s preferred to
     * store the data in column-major order.
     * @param data
     * @return 
     */
    public static AbstractMatrix<RealVector> makeFromRows(double[][] data)
    {
        RealVector[] b = new RealVector[data[0].length];
        for (int c = 0; c < b.length; c++)
        {
            double[] v = new double[data.length];
            for (int r = 0; r < v.length; r++)
            {
                v[r] = data[r][c];
            }
            b[c] = new RealVector(v);
        }
        return new AbstractMatrix<RealVector>( b );
    }
    
    /**
     * Makes a new matrix from data array in column-major order.
     * Vectors are created directly from data, no copies are made.
     * @param data
     * @return 
     */
    public static AbstractMatrix<RealVector> makeFromCols(double[][] data)
    {
        RealVector[] b = new RealVector[data.length];
        for (int c = 0; c < b.length; c++)
        {
            b[c] = new RealVector(data[c]);
        }
        return new AbstractMatrix<RealVector>( b );
    }
    
//    public static RealVector[] makeIdentityVectors(int n)
//    {
//        RealVector[] b = new RealVector[n];
//        for (int c = 0; c < b.length; c++)
//        {
//            double[] v = new double[n];
//            v[c] = 1.0;
//            b[c] = new RealVector(v);
//        }
//        return b;
//    }
    
    public static List<RealVector> makeIdentityVectors(int n)
    {
        List<RealVector> b = new ArrayList<RealVector>();
        for (int c = 0; c < n; c++)
        {
            double[] v = new double[n];
            v[c] = 1.0;
            b.add(c, new RealVector(v) );
        }
        return b;
    }
    
    public static AbstractMatrix<RealVector> makeIdentity(int n)
    {
        return new AbstractMatrix<RealVector>( makeIdentityVectors(n) );
    }

}
